package com.ddshuai.algorithm;

import java.util.Arrays;

/**
 * 描述 直接插入排序
 *
 * @author ddshuai
 * @date 2020-09-18 17:02
 **/
public class InsertSort {
    public static void insertSort(int[] array) {
        for (int i = 2; i < array.length; i++ ) {
            int val = array[i];
            int j = i -1;
            while (j >= 0 && array[j] > val) {  // array[j] > val
                array[j+1] = array[j];
                j--;
            }
            array[j+1] = val; //  array[j+1] 不是array[j]
        }
    }

    public static void main(String[] args) {
        int[] arys = new int[]{0,8,7,5,6,4,3,2,1,9};
        insertSort(arys);
        System.out.println(Arrays.toString(arys));
    }
}
